package kpetruk.tanks2d.server.data;

import org.jbox2d.dynamics.Body;

import kpetruk.tanks2d.server.UnitControl;

public class Unit {
	int id = -1;
	float x, y; //coordinates
	float dirX, dirY; //front direction
	float length,width; //sizes
	float speed = 0.0f;
	float rotationSpeed = 0.0f; //positive-right, negative-left
	boolean bot = false;
	
	//images
	
	//sound
	
	//physics
	Body body = null;
	float maxSpeed = 3;
	float maxRotation = 3;
	float maxTowerRotation = 3;
	float movementForce = 3;
	float rotationForce = 3;
	
	//input and movement variables
	
	//control
	UnitControl uc = null;
	int wantMove = 0;
	int wantRotation = 0;
	int wantTowerRotation = 0;
	
	
	//// methods
	 
	public void addControl(UnitControl uc) {
		this.uc = uc;
	}
	
	public void useAI(){
		if (bot){
			wantMove = 1;
			wantRotation = 1;
			
		}
	}
	
	public String toString() {
		return "Unit ["+id+"] x["+x+"] y["+y+"] speed["+speed+"]";
	}
	
	public void handleInput() {
	  if(wantRotation == 1){
	    body.applyTorque(rotationForce);
	    if(body.getAngularVelocity()>maxRotation){
	      body.setAngularVelocity(maxRotation);
	    }
	  } else if(wantRotation == -1){
      body.applyTorque(-rotationForce);
      if(body.getAngularVelocity()<-maxRotation){
        body.setAngularVelocity(-maxRotation);
      }
    } else {
      body.setAngularVelocity(0);
    }
	}

  public void speedup() {
    // TODO Auto-generated method stub
    
  }

  public void slowdown() {
    // TODO Auto-generated method stub
    
  }

  public void stop() {
    // TODO Auto-generated method stub
    
  }

}
